package com.skeleton.server.blog.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.db.dto.BlogCommentDto;
import com.skeleton.db.dto.UserinfoDto;
import com.skeleton.server.blog.service.IBlogCommentService;
import com.skeleton.utils.SystemConstants;
import com.skeleton.utils.SystemTools;

@Controller
@RequestMapping("blog/comment/")
public class BlogCommentControl {

	protected Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	private IBlogCommentService iBlogCommentService;

	@RequestMapping("addBlogComment")
	@ResponseBody
	public Map<String, Object> addBlogComment(HttpServletRequest request, HttpSession session) {
		logger.info("-------blog/comment/---addBlogComment-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String article_id = request.getParameter("article_id");
		String blog_comment_content = request.getParameter("blog_comment_content");

		UserinfoDto ud = (UserinfoDto) session.getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);

		BlogCommentDto bcDto = new BlogCommentDto();
		bcDto.setCommentId(0);
		bcDto.setLoginname(ud.getUserLoginname());
		bcDto.setBlogId(article_id);
		bcDto.setCommentContent(blog_comment_content);
		bcDto.setCommentCreateDttm(SystemTools.convertCurrentDTTMtoInt());
		bcDto.setCommentCreateIp(request.getRemoteAddr());
		bcDto.setCommentStatus(SystemConstants.BLOG_COMMENT_STATUS_NORMAL);

		ResultDto rd = iBlogCommentService.doAddBlogComment(bcDto);
		if (rd.isSuccess()) {
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

	@RequestMapping("getBlogCommentByArticleId")
	@ResponseBody
	public Map<String, Object> getBlogCommentByArticleId(HttpServletRequest request) {
		logger.info("-------blog/comment/---getBlogCommentByArticleId-----------------");
		Map<String, Object> map = new HashMap<String, Object>();

		String article_id = request.getParameter("article_id");

		BlogCommentDto bcDto = new BlogCommentDto();
		bcDto.setBlogId(article_id);
		bcDto.setCommentStatus(SystemConstants.BLOG_COMMENT_STATUS_NORMAL);

		ResultDto rd = iBlogCommentService.getBlogCommentByArticleId(bcDto);
		if (rd.isSuccess()) {
			List<BlogCommentDto> bcdList = rd.getDtoList("BlogCommentDtoList");
			map.put("bcdList", bcdList);
			map.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			map.put("result", SystemConstants.AJAX_FAILED_ALERT);
			map.put("msg", rd.getErrorMSG());
		}

		return map;
	}

}
